package anshe.object
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	dynamic public class ExTextField extends Sprite
	{
		public var mcText:TextField;		/** 文字内容框 **/
		
		private var baseWidth:Number;		/** 设置时的基础宽度 **/
		private var baseHeight:Number;		/** 设置时的基础高度 **/
		private var textFormat:TextFormat;	/** 字体格式 **/
		
		private var fontBold:Boolean;
		private var fontItalic:Boolean;
		private var fontUnderLess:Boolean;
		private var fontColor:uint;
		private var fontSize:Number;
		private var fontAlign:String;

		public function ExTextField()
		{
			this.cacheAsBitmap = true;
			InitObject();
		}
		
		/** 设置加粗 **/
		public function set isBord(flag:Boolean):void
		{
			textFormat.bold = flag;
			
			this.mcText.defaultTextFormat = textFormat;
			this.text = this.mcText.text;
		}
		
		/** 设置倾斜 **/
		public function set isItalic(flag:Boolean):void
		{
			textFormat.italic = flag;
						
			this.mcText.defaultTextFormat = textFormat;
			this.text = this.mcText.text;
		}
		
		/** 设置下划线 **/
		public function set underline(flag:Boolean):void
		{
			textFormat.underline = flag;

			this.mcText.defaultTextFormat = textFormat;
			this.text = this.mcText.text;
		}
		
		/** 设置文字颜色 **/
		public function set color(col:uint):void
		{
			textFormat.color = col;
			
			this.mcText.defaultTextFormat = textFormat;
			this.text = this.mcText.text;
		}
		
		/** 设置文字字号 **/
		public function set size(num:Number):void
		{
			textFormat.size = num;
			
			this.mcText.defaultTextFormat = textFormat;
			this.text = this.mcText.text;
		}
		
		/** 设置文字对齐方式 **/
		public function set align(str:String):void
		{
			textFormat.align = str;
			
			this.mcText.defaultTextFormat = textFormat;
			this.text = this.mcText.text;
		}
		
		/** 设置文字内容 **/
		public function set text(str:String):void
		{
			this.mcText.width = 4000;	
			this.mcText.height = this.mcText.height * 100;		
			this.mcText.text = str;

			if(this.baseWidth < 0)
			{
				this.mcText.width = this.mcText.textWidth + 6;
			}
			else
			{
				this.mcText.width = this.baseWidth;
			}

			this.onTextChange(null);
		}		
		
		/** 读取文字内容 **/
		public function get text():String
		{
			return this.mcText.text;
		}
		
		/** 设置文字框的大小 **/
		public function SetSize(ww:Number, hh:Number):void
		{
			this.baseWidth = ww;
			this.baseHeight = hh;
			
			this.mcText.width = ww;
			this.mcText.height = hh;
			
			onTextChange(null);
		}
		
		/** 激活文字框,此时文字框大小已经固定,要设置热区为文字框,并允许输入文字 **/
		public function Enabled():TextField
		{
			this.mcText.border = true;
			this.mcText.borderColor = 0x000000;
			
			this.mcText.background = true;
			this.mcText.backgroundColor = 0xFCFCFC;
			
			this.mcText.type = TextFieldType.INPUT;
			this.mcText.text = "";
			
			this.mcText.selectable = true;
			this.mcText.mouseEnabled = true;		
			
			this.stage.focus = this.mcText;

			this.mcText.addEventListener(Event.CHANGE, onTextChange);
			
			onTextChange(null);
			
			return this.mcText;
		}
				
		/** 使文字框不可再被操作 **/
		public function Disabled():void
		{
			this.mcText.removeEventListener(Event.CHANGE, onTextChange);

			this.mcText.border = false;
			this.mcText.background = false;
			this.mcText.selectable = false;
			this.mcText.type = TextFieldType.DYNAMIC;
			this.mcText.mouseEnabled = false;
			
			this.mcText.height = this.mcText.textHeight + 6;
		}
		
		/****************************************************/
		
		private function InitObject():void
		{
			textFormat = new TextFormat();
			textFormat.bold = false;
			textFormat.italic = false;
			textFormat.underline = false;
			textFormat.size = 12;

			this.baseWidth = -1;
			this.baseHeight = -1;
			
			mcText.defaultTextFormat = textFormat;			
			mcText.mouseEnabled = false;
		}	

		/** 当文字内容发生改变时触发 **/
		private function onTextChange(e:Event):void
		{
			var newHeight:Number = this.mcText.textHeight + 6;

			if(this.mcText.type == TextFieldType.INPUT)
			{
				this.mcText.height = this.baseHeight;
				
				if(newHeight > this.baseHeight)
				{
					this.mcText.height = newHeight;
				}
			}
			else
			{
				this.mcText.height = newHeight;
			}
		}
	}
}